﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using System.Configuration;
using System.IO;

namespace TireService.Website.Booking
{
    public partial class OpdaterKalender : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                // Get input (0: date, 1: starttime, 2: endtime, 3: location, 4: subject, 5: description)
                string[] param = Request.QueryString["details"].Split('|');

                DateTime beginDate = Convert.ToDateTime(param[0] + " " + param[1]);
                DateTime endDate = Convert.ToDateTime(param[0] + " " + param[2]);

                string myLocation = param[3];
                string mySubject = param[4];
                string myDescription = param[5];

                // Initialize
                MemoryStream mStream = new MemoryStream();
                StreamWriter writer = new StreamWriter(mStream);
                writer.AutoFlush = true;

                // Create Header
                writer.WriteLine("BEGIN:VCALENDAR");
                writer.WriteLine("PRODID:-//MOBILHJULSKIFT.DK//www.mobilhjulskift.dk//EN");
                writer.WriteLine("BEGIN:VEVENT");

                // Create Body
                writer.WriteLine("DTSTART:" + beginDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
                writer.WriteLine("DTEND:" + endDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
                writer.WriteLine("LOCATION:" + myLocation);
                writer.WriteLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + myDescription);
                writer.WriteLine("SUMMARY:" + mySubject);

                // Create Footer
                writer.WriteLine("PRIORITY:3");
                writer.WriteLine("END:VEVENT");
                writer.WriteLine("END:VCALENDAR");

                // Make it downloadable
                Response.Clear(); // clears the current output content from the buffer
                Response.AppendHeader("Content-Disposition", "attachment; filename=hjulskift.vcs");
                Response.AppendHeader("Content-Length", mStream.Length.ToString());
                Response.ContentType = "application/download";
                Response.BinaryWrite(mStream.ToArray());
                Response.End();

                // Send internal email to ourself on this
                string emailaddress = Convert.ToString(ConfigurationManager.AppSettings["EmailAddress"]);
                bool test = Convert.ToBoolean(Convert.ToByte(ConfigurationManager.AppSettings["TestEnvironmentOn"]));
                string message = String.Format("Outlook aftale gemt " + myLocation + "(" + beginDate.ToString() + "-" + endDate.ToString() + ")");


                Shared emailBLL = new Shared();
                emailBLL.SendEmail(emailaddress, emailaddress, message, "Bruger brugte funktionen", true, test, null, null, null);
            }
            catch (Exception)
            {
                
            }
        }
    }
}